//
// Created by LONG on 2020/10/27.
//

#ifndef CSP_INCLUDE_UTILS_WHEELTIMER_H_
#define CSP_INCLUDE_UTILS_WHEELTIMER_H_

#include "commDef.h"
#include "list.h"

#ifdef __cplusplus
extern "C"
{
#endif

/******************************************************************************
 wheel timer : 时间轮定时器。
 ******************************************************************************/

typedef struct WtWheel S_WtWheel;

enum
{
    TIMER_TYPE_ONCE, /* 只运行一次 */
    TIMER_TYPE_LOOP  /* 循环运行 */
};

enum
{
    TIMER_STATE_START,
    TIMER_STATE_STOP,
    TIMER_STATE_TIMEOUT
};

typedef void (*WheelTimerCallback)(void *arg);
typedef struct WtTimer
{
    S_List node;
    uint32_t initFlag;
    uint32_t runState;
    uint32_t type;
    uint32_t delay;
    WheelTimerCallback callback;
    void *arg;
} S_WtTimer;

S_WtWheel *WT_CreateWheel(const char *name, uint32_t maxDelay);
void WT_DestroyWheel(S_WtWheel *wheel);
int32_t WT_InitTimer(S_WtTimer *timer);
int32_t WT_FinalTimer(S_WtTimer *timer);
int32_t WT_StartTimer(S_WtWheel *wheel, S_WtTimer *timer, WheelTimerCallback callback, void *arg, uint32_t type, uint32_t delay);
int32_t WT_StopTimer(S_WtTimer *timer);
int32_t WT_GetTimerState(S_WtTimer *timer);
void WT_Run(S_WtWheel *wheel, uint32_t step);


#ifdef __cplusplus
}
#endif

#endif //CSP_INCLUDE_UTILS_WHEELTIMER_H_
